using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using System.IO;


namespace YATC
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class HighScore : GameScene
    {
        private List<Player> list;

        public HighScore(Game game)
            : base(game)
        {
            list = new List<Player>();
        }
        
        public void Add(Player player)
        {
            list.Add(player);
            Sort();
            //Save the score in the XML file
            SaveFile();
        }
        private void SaveFile()
        {
            
            var ser = new XmlSerializer(typeof (List<Player>));
            var w = new StreamWriter(@".\\Content\SaveGame.xml");
            ser.Serialize(w,list);
            w.Close();
        }
        private void Sort()
        {
            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    if (list[i].score < list[j].score)
                    {
                        var n = list[i];
                        list[i] = list[j];
                        list[j] = n;
                    }
                }
            }
        }
        public List<Player> List
        {
            get { return list; }
            set{ list = value;}
        }
        public override void Show()
        {
            Game.IsMouseVisible = true;
            base.Show();
        }
       
        internal void Reset()
        {
            list.Clear();
            //Save the score in the XML file
            SaveFile();
        }
    }
}